<?php
/**
 * Update Customer
 */

use Fox\Generator;
use Fox\Html\Button;
use Fox\Common\Customer\Partner;
use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\Html\TabPanel\Tab,Fox\Html\Wrapper,Fox\Html\TabPanel,Fox\Html\Screen;
use Fox\Common\Customer;
$xview = Xview::instance();
$id = $xview->val("customerid");
$mode = $xview->val('mode');
try {
	$customer = new Customer($id);
	$data = $customer->getData();
	$data['gPartners'] = array();
	$data['gParents'] = $customer->parents();
	foreach($customer->partners() as $parner)
	{
		$data['gPartners'][] = $parner->getData();
	}

}
catch(Fox\Object\Exception\NotExist $e)
{
	var_dump($e);
}
if(!$customer->exists())
{
	Xview::instance()->systemError();
}
if($mode == 'employee') { 
	$title = "Fiche intervenant";
} elseif($mode == 'entity') {
	$title = "Fiche entit&eacute;";
}else {
	$title = Msg(Customer::BUNDLE_NAME,17);
}
$screen = new Screen("eCustomer","update");
$screen->pageTitle($title);
$screen->add(new Fox\Html\Button("idele",Generator::display("delete","?mod=customer&act=delete&customerid=$id")))
		->color(Fox\Html\Button::COLOR_RED);
$tabPanel = new TabPanel("pCustomer");
$wMasterData = new Wrapper("wMasterData");
$blHeader = Xview::instance()->component("customer", "header");
if($mode == 'employee') {
	
} elseif($mode == 'entity') {

} else {
	$wMasterData->add($blHeader);
}
$blName = Xview::instance()->component("customer", "name");
$wMasterData->add($blName);
$blAddress = Xview::instance()->component("customer", "address");
$wMasterData->add($blAddress);
$tabPanel->add(new Tab("tMasterData",Msg(Fox\Msg::BUNDLE,Fox\Msg::TITLE_MASTER_DATA),$wMasterData));
$blPartners = Xview::instance()->component("customer", "partners",array('mode'=>$mode));

if($mode == 'employee') {
	
} else {
	$tabPanel->add(new Tab("tPartners",Msg(Customer::BUNDLE_NAME,40),$blPartners));
}
$blParents = Xview::instance()->component("customer", "attached");
if(count($data['gParents']) > 0)
{
	$tabPanel->add(new Tab("tParents",Msg(Customer::BUNDLE_NAME,43),$blParents));
}
$screen->add($tabPanel);
$screen->initialize($data);

if($screen->submitted())
{
	$inputs = $screen->get();
	//
	if(!$screen->errors())
	{
		try {
			$partners = $customer->partners();
			// Manage Partners
			if(!empty($inputs["gPartners"])) {
				foreach($inputs["gPartners"] as $key => $partnerData)
				{
					if(empty($partnerData['partnerid']))
					{
						unset($inputs["gPartners"][$key]);
						$blPartners->item("gPartners")->setDatas($inputs["gPartners"]);
						continue;
					}
					if(isset($partnerData['defaultflag'])) {
						if($partnerData['defaultflag'] == 't')
						{
							$partnerData['defaultflag'] = 1;
						}
						elseif($partnerData['defaultflag'] == 'f')
						{
							$partnerData['defaultflag'] = 0;
						}
					} else {
						$partnerData['defaultflag'] = 0;
					}
					$partner = new Partner();
					if(empty($partnerData['role']))
					{
						$partnerData['role'] = Role::CONTACT_PERSON;
					}
					$partnerData['customerid'] = $customer->getId();
					$partner->setFields($partnerData);
					if(isset($partners[$partnerData['partnerid']]))
					{
						unset($partners[$partnerData['partnerid']]);
					}
					$customer->partner($partner);
				}
			}
			foreach($partners as $partner)
			{
				$partner->delete();
			}
			// Create Customer
			$customer->setFields($inputs);
			$customer->prepareUpdate(Customer::CHANGE_DEFAULT);
			$customer->update();
			Fox\Message::Information(Msg(Fox::MSGCL,12,"<b>".$customer->name1."</b>"));
		//	Xview::instance()->redirect("?mod=customer&act=update&customerid=$id");
		}
		catch(Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e->getMessage());
		}
		catch(Exception $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}
}

echo $screen->Display();